require(['common','contact','formedit'], function () {
  // 底部详情
  var priceData = {
    insurance: [
      {con: '外籍人士早餐附加费外籍人士早餐附加…',price: 899,num: 3},
    ],
    serve: [
      {con: '外籍人士早餐附加费外籍人士早餐附加…',price: 8999,num: 3},
      {con: '可选服务',price: 899,num: 20},
      {con: '可选服务',price: 89,num: 2}
    ],
    discounts: [
      {con: '早点优惠',price: 2000},
      {con: '优惠信息',price: 200}
    ]
  };
  var priceDetail = _.template($("#order-price-detail").html())(priceData);
  $(".gzl-footbar__bd").footPopup({
    title: '费用明细',
    titleAlign: 'center',
    content: priceDetail,
    line: false,
  });
  // 成人、儿童、长者
  $(".booking-numlist .gzl-count span").on("click", function () {
    if (!$(this).hasClass("disabled")) {
      var numlist, total, max, min, num, val, type, selTpl, sellist;
      numlist = $(this).parents(".booking-numlist");
      total = numlist.data("total");
      max = numlist.data("max");
      min = numlist.data("min");
      num = $(this).parent().find(".gzl-count__num");
      val = parseInt(num.text());
      type = $(this).parent().data("type");
      selTpl = _.template($("#tourist-select").html())({ type: type });
      sellist = $(".tourist-sel-list");
      if ($(this).hasClass("gzl-count__plus")) {// 加
        if (total < max) {
          $(this).parent().find(".gzl-count__minus").removeClass("disabled");
          num.text(val + 1);
          numlist.data("total",total + 1);
          if (total + 1 === max) {
            numlist.find(".gzl-count__plus").addClass("disabled");
          }
          if (type === "adult") {
            if ($("[data-type='adult']", sellist).length < 1) {
              $(selTpl).prependTo(sellist);
            }
          } else if (type === "child") {
            if ($("[data-type='child']", sellist).length < 1) {
              if ($("[data-type='adult']", sellist).length < 1) {
                $(selTpl).prependTo(sellist);
              } else {
                $("[data-type='adult']", sellist).after(selTpl);
              }
            }
          } else {
            if ($("[data-type='elder']", sellist).length < 1) {
              sellist.append(selTpl);
            }
          }
        }
      } else if ($(this).hasClass("gzl-count__minus")) {// 减
        if (total > min) {
          numlist.find(".gzl-count__plus").removeClass("disabled");
          num.text(val - 1);
          numlist.data("total",total - 1);
          if (val - 1 === 0) {
            $(this).addClass("disabled");
            console.log(type)
            if (type === "adult") {
              console.log(1)
              $("[data-type='adult']", sellist).remove();
            } else if (type === "child") {
              console.log(2)
              $("[data-type='child']", sellist).remove();
            } else {
              console.log(3)
              $("[data-type='elder']", sellist).remove();
            }
          }
        }
      }
    }
  })
  // 联系人选择
  $(".tourist-sel-list").on("click", "li", function () {
    $.contact({
      type: $(this).data("type"),
      totality: 0,
      list: [{
        index: 0,checkType: null, checkState: 'uncheck',name: '克里斯蒂亚诺·罗纳', mobile: '13800138000',cardTypeName: '身份证',cardNm: '441246195505058984'
      }, {
        index: 1,checkType: null, checkState: 'uncheck',name: '克里斯蒂亚诺', mobile: '13800138001',cardTypeName: '身份证',cardNm: '440111198410201100'
      }, {
        index: 2,checkType: null, checkState: 'uncheck',name: '罗纳', mobile: '13800138002',cardTypeName: '港澳通行证',cardNm: '441246195505058984'
      }, {
        index: 3,checkType: null, checkState: 'uncheck',name: '测试',cardTypeName: '港澳通行证',cardNm: '441246195505058984'
      }],
      target: $(".booking-list-bd>li"),
      confirmFn: function (tourist,self) {
        console.log(tourist)
        var touristList = $(".tourist-info-list");
        var tpl = _.template($("#tourist-list").html())(tourist);
        if($("li",touristList).length < 1){
          $(tpl).appendTo(touristList);
        }else if(tourist.type === 'adult'){
          $(".adult-info",touristList).remove();
          $(tpl).prependTo(touristList);
        }else if(tourist.type === 'child'){
          $(".child-info",touristList).remove();
          if($(".adult-info",touristList).length < 1){
            $(tpl).prependTo(touristList);
          } else {
            $(".adult-info",touristList).after(tpl);
          }
        }else if(tourist.type === 'elder'){
          $(".elder-info",touristList).remove();
          touristList.append(tpl);
        }
        $(".contact-select-page").hidePopup();
      },
      editsaveFn: function () {
        console.log(123123123123)
        $(".gzl-formedit").hidePopup();
      }
    });
  })
  // 保险
  $(".travel-insurance").on("click", function () {
    $.pushHistory();
    var insuranceSel = $(_.template($("#insurance-select").html())()).appendTo(document.body);
    insuranceSel.show().addClass("show");
    $(".insurance-check", insuranceSel).on("click", function () {// 勾选
      var icon = $(this).find("use");
      var checkStatus = icon.attr("xlink:href");
      if(checkStatus !== "#icon-checked"){
        if (checkStatus === "#icon-uncheck") {
          icon.attr("xlink:href","#icon-check").parent().addClass("checked");
        }else if (checkStatus === "#icon-check"){
          icon.attr("xlink:href","#icon-uncheck").parent().removeClass("checked");
        }
      }
    })
    $(".confirm-btn", insuranceSel).on("click", function () {// 确定
      insuranceSel.hidePopup();
    })
    $(".insurance-info.explain", insuranceSel).on("click", function () {// 保险说明
      $.pushHistory();
      var explain = $(_.template($("#insurance-explain").html())()).appendTo(document.body);
      explain.show().addClass("show");
      $(".gzl-navbar__item", explain).on("click", function () {
        $(this).addClass("active").siblings().removeClass("active");
        $(".explain-item", explain).hide().eq($(this).index()).show();;
      })
    })
  })
  // 发票
  $(".invoice-select .gzl-cell__bd").on("click", function () {
    $.pushHistory();
    var data = {
      type: '个人'
    }
    var invoiceSel = $(_.template($("#invoice-select").html())(data)).appendTo(document.body);
    invoiceSel.show().addClass("show");
    $(".gzl-switch",invoiceSel).on("change", function () {
      if ($(this).is(":checked")) {
        $(".invoice-con", invoiceSel).show();
      } else {
        $(".invoice-con", invoiceSel).hide();
      }
    })
    $("input[name='invoice-type']",invoiceSel).on("change", function () {
      var type = $(this).next().html();
      if (type === '个人') {
        $(".duty-paragraph",invoiceSel).addClass("hide");
      }else{
        $(".duty-paragraph",invoiceSel).removeClass("hide");
      }
    })
    $(".invoice-btn .btn",invoiceSel).on("click", function(){
      $(".invoice-select-page").hidePopup();
    })
  })
  // 返回监听
  $.popstate(function () {
    var contactSel = $(".contact-select-page").length < 1;
    var contactEdit = $(".gzl-formedit").length < 1;
    var insuranceSel = $(".insurance-select-page").length < 1;
    var insuranceExplain = $(".insurance-explain-page").length < 1;
    var invoiceSel = $(".invoice-select-page").length < 1;
    if (contactSel && contactEdit && insuranceSel && insuranceExplain && invoiceSel) {
      window.history.go(-1);
    } else {
      if (!contactEdit) {// 新增、编辑联系人
        $(".gzl-formedit").hidePopup();
      } else {// 联系人
        if(!contactSel){
          $(".contact-select-page").hidePopup();
        }
      }
      if (!insuranceExplain) {// 保险说明
        $(".insurance-explain-page").hidePopup();
      } else {
        if (!insuranceSel) {// 保险
          $(".insurance-select-page").hidePopup();
        }
      }
      if (!invoiceSel) {// 发票
        $(".invoice-select-page").hidePopup();
      }
    }
  })
});